﻿using Coldairarrow.Entity.Base_AutoReplys;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_AutoReplys
{
    public class Base_AutoReplyBusiness : BaseBusiness<Base_AutoReply>, IBase_AutoReplyBusiness, ITransientDependency
    {
        public Base_AutoReplyBusiness(IDbAccessor repository)
            : base(repository)
        {
        }

        #region 外部接口

        public async Task<PageResult<Base_AutoReply>> GetDataListAsync(PageInput<GetTSearchDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_AutoReply>();
            var search = input.Search;

            //分组筛选
            if (!search.TypeId.IsNullOrEmpty())
            {
                where = where.And(x => x.KeepOne.Equals(search.TypeId));
            }

            //关键词筛选
            if (!search.Search.IsNullOrEmpty())
            {
                where = where.And(x => x.KeyWord.Equals(search.Search) || x.Contents.Equals(search.Search));
            }


            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<AutoReplyDTO> GetTheModelAsync(string id)
        {
            AutoReplyDTO model = new AutoReplyDTO();
            var data = await GetEntityAsync(id);
            model.Id = data.Id;
            model.KeyWord = data.KeyWord;
            model.Contents = data.Contents;
            model.CreateTime = data.CreateTime;
            model.ImgURL = data.ImgURL;
            model.LinkURL = data.LinkURL;

            model.ChildList = Service.GetIQueryable<Base_AutoReplyDetail>().Where(x => x.ReplyId.Equals(data.Id)).ToList();

            return model;
        }

        public async Task<Base_AutoReply> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(Base_AutoReply data)
        {
            var aCount = GetIQueryable().Where(x => x.KeyWord.Equals(data.KeyWord)).Count();
            if (aCount > 0)
            {
                throw new BusException("该关键词回复已存在，请修改后重试！");
            }
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(Base_AutoReply data)
        {
            var aCount = GetIQueryable().Where(x => x.KeyWord.Equals(data.KeyWord) && !x.Id.Equals(data.Id)).Count();
            if (aCount > 0)
            {
                throw new BusException("该关键词回复已存在，请修改后重试！");
            }
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            //删除其子集内容
            foreach (var itm in ids)
            {
                var dLsit = Service.GetIQueryable<Base_AutoReplyDetail>().Where(x => x.ReplyId.Equals(itm)).Select(x => x.Id).ToList();
                if (dLsit.Count > 0)
                {
                    await Service.DeleteAsync<Base_AutoReplyDetail>(dLsit);
                }        
            }

            await DeleteAsync(ids);
             

        }

        #endregion

        #region 私有成员

        #endregion
    }
}